using System;
using System.Collections.Generic;
using Mall.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Mall.Api.ParamModel;
using Newtonsoft.Json;
using Mall.Api.Repository;
using Mall.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Mall.Api.Filters;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;

namespace Mall.Api.Controllers
{
    /// <summary>
    /// 商品详情页信息
    /// </summary>


    // [Authorize]
    [ApiController]
    [Route("purchare")]
    public class PurchareController : ControllerBase
    {
        private IRepository<Commodity> _commodity;
        private IRepository<Supplier> _supplier;
        private IRepository<Brand> _brand;
        private IRepository<Class> _class;
        private IRepository<Warehouse> _warehouse;
        private IRepository<CommodityWarehouse> _commodityWarehouse;
        private IRepository<CommodityProperty> _commodityProperty;
        private IRepository<CommodityPropertyValue> _commodityPropertyValue;
        private IRepository<CommodityResources> _commodityResources;
        private IRepository<Resource> _resource;
        private IRepository<CommoditySKU> _commoditySKU;



        private TokenParameter _tokenParameter;
        private readonly IConfiguration _configuration;


        public PurchareController(IConfiguration configuration, IRepository<Commodity> commodity, IRepository<Supplier> supplier,
        IRepository<Brand> brand, IRepository<Class> classes, IRepository<Warehouse> warehouse, IRepository<CommodityWarehouse> commodityWarehouse,
        IRepository<CommodityProperty> commodityProperty, IRepository<CommodityPropertyValue> commodityPropertyValue,
        IRepository<CommodityResources> commodityResources, IRepository<Resource> resource, IRepository<CommoditySKU> commoditySKU)
        {
            _configuration = configuration;
            _commodity = commodity;
            _supplier = supplier;
            _warehouse = warehouse;
            _commodityWarehouse = commodityWarehouse;
            _commodityProperty = commodityProperty;
            _commodityPropertyValue = commodityPropertyValue;
            _commodityResources = commodityResources;
            _commoditySKU = commoditySKU;
            _resource = resource;
            _class = classes;
            _brand = brand;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }


        /// <summary>
        /// 获取指定商品详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {





            var ckn = from c in _commoditySKU.Table
                      join d in _commodity.Table on c.CommodityId equals d.Id
                      join p in _commodityProperty.Table on c.CommodityPropertyId equals p.Id
                      join pv in _commodityPropertyValue.Table on c.CommodityPropertyValueId equals pv.Id
                      where c.CommodityId == id
                      select new
                      {
                          CommodityId = c.CommodityId,
                          PropertyId = c.CommodityPropertyId,
                          PropertyName = p.PropertyName,
                          PropertyValueId = c.CommodityPropertyValueId,
                          PropertyValue = pv.PropertyValue,
                          Price = pv.ValuePrice,
                          productName = d.ProductName
                      };
            var f = ckn.ToList();

            var csku = from sku in ckn
                       group sku by new { sku.PropertyName, sku.PropertyId }
                       into s
                       select new { PropertyName = s.Key.PropertyName, s.Key.PropertyId, Count = s.Count() };
            var h = csku.ToList();

            var propertys = new List<Property>();
            foreach (var item in h)
            {
                var propertyValues = new List<PropertyValue>();

                foreach (var itemckn in f)
                {
                    if (itemckn.PropertyName == item.PropertyName)
                    {
                        propertyValues.Add(new PropertyValue
                        {
                            Id = itemckn.PropertyValueId,
                            Value = itemckn.PropertyValue,
                            Price = itemckn.Price.ToString()
                        });
                    }
                }
                propertys.Add(new Property
                {

                    Id = item.PropertyId,
                    PropertyName = item.PropertyName,
                    PropertyValues = propertyValues

                });

            }
            var commodity = _commodity.GetById(id);

            return new
            {
                Code = 200,
                Data = new
                {
                    commodity,
                    propertys
                },
                Msg = "获取指定商品成功^_^"
            };

        }




    }


}
